package com.ghck.server.service.impl;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ghck.server.dao.BodypartDictMapper;
import com.ghck.server.model.BodypartDict;
import com.ghck.server.model.Symptom;
import com.ghck.server.model.query.BodypartDictQuery;
import com.ghck.server.model.response.BodypartDictRes;
import com.ghck.server.model.response.IdNameRes;
import com.ghck.server.service.BodypartDictService;
import com.ghck.server.service.DiseaseService;
import com.ghck.server.utils.RedisKeyUtil;

/**
 * 身体部位  <br>
 * dict_bodypart <br>
 * v1.0 2016-05-01
 */
@Service
@Transactional
public class BodypartDictServiceImpl implements BodypartDictService {

	private static final Logger logger = LoggerFactory.getLogger(BodypartDictServiceImpl.class);

	@Autowired
	private BodypartDictMapper bodypartDictMapper;

	@Autowired
	private DiseaseService diseaseService;

	@Autowired
	RedisTemplate<String,Object> template;

	/**
	 *根据ID删除 身体部位
	 */
	@Override
	public Integer deleteBodypartDictById(Integer id){
		return bodypartDictMapper.deleteById(id);
	}

	/**
	 *新增 身体部位
	 */
	@Override
	@Transactional
	public Integer batchInsertBodypartDict(List<? extends BodypartDict> bodypartDicts){
		Integer count = bodypartDictMapper.batchInsert(bodypartDicts);
		return count;
	}
	/**
	 *新增 身体部位
	 */
	@Override
	@Transactional
	public Integer insertBodypartDict(BodypartDict domain){
		Integer id = bodypartDictMapper.insert(domain);
		return id;
	}

	/**
	 *更新 身体部位
	 */
	@Override
	@Transactional
	public Integer updateBodypartDict(BodypartDict domain){
		Integer count = bodypartDictMapper.update(domain);
		return count;
	}

	/**
	 *根据ID查询 身体部位
	 */
	public BodypartDict findBodypartDictById(Integer id){
		BodypartDict result = bodypartDictMapper.findById(id);
		if(result!=null){
			result.dealWith();
		}
		return result;
	}

	/**
	 *通过query 查询 身体部位
	 */
	public List<BodypartDict> findBodypartDictByQuery(BodypartDictQuery query){
		query.dealWith();
		List<BodypartDict> result = bodypartDictMapper.findByQuery(query);
		if(CollectionUtils.isNotEmpty(result)) {
			for (BodypartDict bodypartDict : result) {
				bodypartDict.dealWith();
			}
		}
		return result;
	}	

	/**
	 *通过query count总条数
	 */
	public Integer countBodypartDictByQuery(BodypartDictQuery query){
		query.dealWith();
		Integer result = bodypartDictMapper.countByQuery(query);
		return result;
	}

	/**
	 * 通过ids查询
	 */
	public List<BodypartDict> findBodypartDictByIds(List<Integer> ids){
		List<BodypartDict> result = bodypartDictMapper.findByIds(ids);
		if(CollectionUtils.isNotEmpty(result)) {
			for (BodypartDict bodypartDict : result) {
				bodypartDict.dealWith();
			}
		}
		return result;
	}

	/**
	 *根据name查询 身体部位
	 */
	public BodypartDict findByName(String name){
		BodypartDict result = bodypartDictMapper.findByName(name);
		if(result!=null){
			result.dealWith();
		}
		return result;
	}

	/**
	 *获取所有字典 身体部位
	 */
	public List<BodypartDictRes> findAll(){
		List<BodypartDictRes> result = bodypartDictMapper.findAll();
		if(CollectionUtils.isNotEmpty(result)) {
			for (BodypartDictRes bodypartDict : result) {
//				List<IdNameRes> diseaseList = diseaseService.findDiseaseByBodypartId(bodypartDict.getId());
				//bodypartDict.setDiseaseList(diseaseList);
				List<Symptom> symptomList = diseaseService.findSymptonListByBodypartId(bodypartDict.getId());
				bodypartDict.setSymptomList(symptomList);
			}
		}
		return result;
	}


	public List<BodypartDictRes> findAllWithCache(){
		String key = RedisKeyUtil.bodypartHashKey();
		if(!template.hasKey(key)){
			syncInRedis(true);
		}

		HashOperations<String, String, BodypartDictRes> bodyhashOperations = template.opsForHash();
		List<BodypartDictRes> bodyparts = bodyhashOperations.values(key);
		if (CollectionUtils.isNotEmpty(bodyparts)) {
			String bodyIdMapDiseaseIdNameKey = RedisKeyUtil.bodyIdMapDiseaseIdNameList();
			if(!template.hasKey(bodyIdMapDiseaseIdNameKey)){
				diseaseService.syncInRedis(true);
			}
			HashOperations<String, String, List<IdNameRes>> aa = template.opsForHash();
			Map<String,List<IdNameRes>> subMap = aa.entries(bodyIdMapDiseaseIdNameKey);
			if(subMap!=null){
				for (BodypartDictRes temp : bodyparts) {
					String hashKey = RedisKeyUtil.domainIdKey(temp.getId());
					if(subMap.containsKey(hashKey)){
						List<IdNameRes> list = subMap.get(hashKey);
						temp.setDiseaseList(list);
					}
				}

			}
		
			Collections.sort(bodyparts);
		}
		
		return bodyparts;
	}



	public void syncInRedis(boolean force){
		String key = RedisKeyUtil.bodypartHashKey();
		if(force || (!template.hasKey(key) )){
			List<BodypartDictRes> result = bodypartDictMapper.findAll();
			if (CollectionUtils.isNotEmpty(result)) {
				Map<String, BodypartDictRes> map = new HashMap<String, BodypartDictRes>();
				for (BodypartDictRes bodypartDictRes : result) {
					String hashKey = RedisKeyUtil.domainIdKey(bodypartDictRes.getId());
					map.put(hashKey, bodypartDictRes);
				}
				if(!map.isEmpty()){
					template.delete(key);
					template.opsForHash().putAll(key, map);
//					template.expire(key, 30, TimeUnit.DAYS);
				}
			}
		}
	}
}	